package me.firstapp.spi.shop.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.JsonPage;
import me.firstapp.common.json.PageObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.module.shop.JsonSku;
import me.firstapp.spi.base.impl.BaseServiceImpl;
import me.firstapp.spi.shop.SkuService;

@Service
public class SkuServiceImpl extends BaseServiceImpl implements SkuService {

	@Override
	public PageObject<JsonSku> findSkuPage(Integer pageNo, Integer pageSize, Map<String, Object> searchParams,
			String sortParam, String sortType) {
		PageObject<JsonSku> result = new PageObject<>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("pageNo", pageNo);
			parameters.put("pageSize", pageSize);
			parameters.put("sortParam", sortParam);
			parameters.put("sortType", sortType);
			JSONObject jsonObject = apiBody(parameters, "/sku/findSkuPage.do", "分页查询sku", searchParams);
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(jsonObject.getString("code"))) {
				JSONObject pageJsonObject = jsonObject.getJSONObject("page");
				if (pageJsonObject != null) {
					JsonPage<JsonSku> jsonPage = new JsonPage<>();
					jsonPage.setPageNo(pageJsonObject.getIntValue("pageNo"));
					jsonPage.setPageSize(pageJsonObject.getIntValue("pageSize"));
					jsonPage.setTotalCount(pageJsonObject.getIntValue("totalCount"));
					List<JsonSku> skuList = new ArrayList<>();
					JSONArray jsonArray = pageJsonObject.getJSONArray("list");
					if (jsonArray != null) {
						for (int i = 0; i < jsonArray.size(); i++) {
							JSONObject _jsonObject = jsonArray.getJSONObject(i);
							if (_jsonObject != null) {
								JsonSku sku = JSONObject.toJavaObject(_jsonObject, JsonSku.class);
								skuList.add(sku);
							}
						}
					}
					jsonPage.setList(skuList);
					result.setPage(jsonPage);
					result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				}
			} else {
				throw new ServiceException(jsonObject.getString("code"), jsonObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<JsonSku> addSku(Long productId, String name) {
		SingleObject<JsonSku> result = new SingleObject<>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("productId", productId);
			parameters.put("name", name);

			JSONObject resultJson = api(parameters, "/sku/addSku.do", HttpMethod.POST, "新增sku");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultJson.getString("code"))) {
				JsonSku apiJsonSku = JSONObject.toJavaObject(resultJson.getJSONObject("object"), JsonSku.class);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(apiJsonSku);
			} else {
				throw new ServiceException(resultJson.getString("code"), resultJson.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<JsonSku> updateSku(Long skuId, String name) {
		SingleObject<JsonSku> result = new SingleObject<>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("skuId", skuId);
			parameters.put("name", name);

			JSONObject resultJson = api(parameters, "/sku/updateSku.do", HttpMethod.POST, "更新sku");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultJson.getString("code"))) {
				JsonSku apiJsonSku = JSONObject.toJavaObject(resultJson.getJSONObject("object"), JsonSku.class);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(apiJsonSku);
			} else {
				throw new ServiceException(resultJson.getString("code"), resultJson.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<Object> deleteSkus(String ids) {
		SingleObject<Object> result = new SingleObject<>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("skuIds", ids);

			JSONObject resultJson = api(parameters, "/sku/deleteSku.do", HttpMethod.POST, "删除sku");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultJson.getString("code"))) {
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			} else {
				throw new ServiceException(resultJson.getString("code"), resultJson.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}



}
